import 'package:cbec_app/components/base/base_ctr.dart';
import 'package:cbec_app/components/base/base_image.dart';
import 'package:cbec_app/components/http/network.dart';
import 'package:cbec_app/routes/routes.dart';
import 'package:cbec_app/utils/AppData.dart';
import 'package:cbec_app/utils/Tools.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

///会员页控制器
class MemberController extends BaseController with SingleGetTickerProviderMixin {
  late TabController typeTabController;

  //积分明细页码
  int page = 1;

  //积分类型 0 全部 1 收入 2 支出
  dynamic type = null;

  //积分数据
  List logList = [];

  bool hasNextPage = true;

  final String nickname = AppData.nickname ?? '未登录用户';

  //子卡列表
  List childrenCardList = [];

  //选中的主卡
  int selectedMainCard = 0;

  //会员卡
  List cardList = [];

  var selected = 0.obs;

  Map vipInfo = {}; //会员信息

  //任务列表
  List taskList = [];

  @override
  void onInit() {
    // getMemberPage();
    getVipCardList();
    getVipInfo();
    //---------消费记录类型---------
    typeTabController = TabController(length: 3, vsync: this);
    typeTabController.addListener(() {
      if (typeTabController.indexIsChanging) {
        // 当tab切换时执行的操作
        print("切换了类型，当前类型为 index: ${typeTabController.index}");
        switch (typeTabController.index) {
          case 0:
            type = null;
            break;
          case 1:
            type = 0;
            break;
          case 2:
            type = 1;
            break;
          default:
            type = null;
        }
        getPointLog();
      }
    });
  }

  ///获取会员信息
  Future getVipInfo() async {
    Tools.showLoading();
    return Network.getVipInfo({}).then((value) {
      print("cccccc:$value");
      vipInfo = value;
      taskList = value['userLevelTask'];
      update();
      Tools.hideLoading();
    }).catchError((onError) {
      print(onError);
      Tools.hideLoading();
    });
  }

  ///获取会员装修
  Future getMemberPage() async {
    Tools.showLoading();
    return Network.getMemberPage().then((value) {
      print(value);
      update();
      Tools.hideLoading();
    }).catchError((onError) {
      print(onError);
      Tools.hideLoading();
    });
  }

  ///获取会员信息
  Future getVipCardList() async {
    Tools.showLoading();
    return Network.getVipcardList({}).then((value) {
      print("这:$value");

      cardList = value;
      if (cardList.isNotEmpty) {
        childrenCardList = cardList[selectedMainCard]['priceInfo'];
      }

      update();
      Tools.hideLoading();
    }).catchError((onError) {
      print(onError);
      Tools.hideLoading();
    });
  }

  //主卡选中
  void choiseMainCard(int index) {
    print(index);
    selectedMainCard = index;
    childrenCardList = cardList[selectedMainCard]['priceInfo'];
    print("这里$childrenCardList");
    update();
  }

  //跳转到积分明细页面
  void scoreLog() {
    getPointLog();
    Get.toNamed(Routes.scoreLog);
  }

  ///获取地址列表
  Future getPointLog() async {
    Tools.showLoading();
    final Map<String, dynamic> params = {"pageNum": page, "pageSize": 10, "sid": AppData.sid};
    if (type != null) {
      params['type'] = type;
    }
    return Network.getPointLog(params).then((value) {
      print(value);
      hasNextPage = value['hasNextPage'];
      if (page > 1) {
        logList.addAll(value['list']);
      } else {
        logList = value['list'];
      }
      update();
      Tools.hideLoading();
    }).catchError((onError) {
      print(onError);
      Tools.hideLoading();
    });
  }

  //弹窗二维码
  void alterQrCode(context) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          contentPadding: EdgeInsets.zero,
          content: BaseImage(url: "https://img2.baidu.com/it/u=4005115713,4032301397&fm=253&fmt=auto&app=138&f=GIF?w=200&h=200", width: 200.w),
        );
      },
    );
  }

  @override
  void onPageRefresh() async {
    super.onPageRefresh();
    page = 1;
    await getPointLog();
    baseRefreshCtr.refreshCompleted(resetFooterState: true);
  }

  @override
  void onPageLoading() async {
    super.onPageLoading();
    if (hasNextPage) {
      page += 1;
      await getPointLog();
      baseRefreshCtr.loadComplete();
    }
  }

  ///选中哪张卡
  void select(int index) {
    selected.value = index;
    update();
  }

  //买
  void buy() {
    var data = cardList[selectedMainCard];
    if (data['receiveType'] == 1) {
      Tools.showToast("要花钱买");
    } else {
      Tools.showToast("直接免费送");
    }
    print(data);
  }

  @override
  void onClose() {
    typeTabController.dispose();
    super.onClose();
  }
}
